package net.chenlin.dp.modules.task;

import lombok.extern.slf4j.Slf4j;
import net.chenlin.dp.common.utils.CommonUtils;
import net.chenlin.dp.modules.business.dao.DeviceCalibrationMapper;
import net.chenlin.dp.modules.business.entity.po.deviceCalibration.DeviceCalibrationDetailPo;
import net.chenlin.dp.modules.business.entity.vo.deviceCalibration.CountRatioVo;
import net.chenlin.dp.modules.business.entity.vo.deviceCalibration.DeviceCalibrationDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Yangsm
 * @ClassName: NewDeviceCalibrationDetailTask
 * @Description: TODO
 * @date 2023年12月29日 15:32
 */
@Slf4j
@Component
public class NewDeviceCalibrationDetailTask {

    @Autowired
    private DeviceCalibrationMapper mapper;

    @Transactional(rollbackFor = Exception.class)
    //@Scheduled(cron = "0 0/1 * * * ?")
    //@Scheduled(cron = "0 */1 * * * ?")
    public void getCountDeviceMeteredElectricity() {

        log.info("开始  === 计算校准详情表 ---- 校准系数");

        // 获取需要计算的设备编号
        List<DeviceCalibrationDetailVo> deviceNoList = mapper.getDeviceNoAndTime();
        deviceNoList.stream().forEach(detailVo -> {

            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date calibrationStartTime = detailVo.getCalibrationStartTime();
            String stringStartTime = dateFormat.format(calibrationStartTime);
            Date calibrationEndTime = detailVo.getCalibrationEndTime();
            String stringEndTime = dateFormat.format(calibrationEndTime);

            BigDecimal start = mapper.getStartCountPower(stringStartTime);
            BigDecimal end = mapper.getEndCountPower(stringEndTime);

            BigDecimal result = end.subtract(start);

            CountRatioVo countRatioVo = mapper.getDeviceCoubnt(detailVo.getDeviceNo(),stringStartTime,stringEndTime);
            if(null != countRatioVo ){

                System.out.println(result);
                countRatioVo.setCalibrationDifference(result);

                BigDecimal multiply = result.multiply(new BigDecimal(11));

                BigDecimal aPhaseRatio = multiply.divide(countRatioVo.getAPhaseCount(), 3, RoundingMode.HALF_UP);
                countRatioVo.setAPhaseRatio(aPhaseRatio);

                BigDecimal bPhaseRatio = multiply.divide(countRatioVo.getBPhaseCount(), 3, RoundingMode.HALF_UP);
                countRatioVo.setBPhaseRatio(bPhaseRatio);

                BigDecimal cPhaseRatio = multiply.divide(countRatioVo.getCPhaseCount(), 3, RoundingMode.HALF_UP);
                countRatioVo.setCPhaseRatio(cPhaseRatio);

                List<DeviceCalibrationDetailPo> insertList = new ArrayList<>();

                DeviceCalibrationDetailPo poA = new DeviceCalibrationDetailPo();

                poA.setDeviceNo(detailVo.getDeviceNo());
                poA.setPhaseSequence("A");
                poA.setDeviceMeteredElectricity(countRatioVo.getAPhaseCount());
                poA.setDeviceMeteredTime(new Date());
                poA.setBaseAmmeteMeteredElectricity(result);
                poA.setBaseAmmeteTime(new Date());
                poA.setCalibrationFactor(aPhaseRatio);
                poA.setCalibrationStartTime(calibrationStartTime);
                poA.setCalibrationEndTime(calibrationEndTime);
                insertList.add(poA);

                DeviceCalibrationDetailPo poB = new DeviceCalibrationDetailPo();

                poB.setDeviceNo(detailVo.getDeviceNo());
                poB.setPhaseSequence("B");
                poB.setDeviceMeteredElectricity(countRatioVo.getBPhaseCount());
                poB.setDeviceMeteredTime(new Date());
                poB.setBaseAmmeteMeteredElectricity(result);
                poB.setBaseAmmeteTime(new Date());
                poB.setCalibrationFactor(bPhaseRatio);
                poB.setCalibrationStartTime(calibrationStartTime);
                poB.setCalibrationEndTime(calibrationEndTime);
                insertList.add(poB);

                DeviceCalibrationDetailPo poC = new DeviceCalibrationDetailPo();

                poC.setDeviceNo(detailVo.getDeviceNo());
                poC.setPhaseSequence("C");
                poC.setDeviceMeteredElectricity(countRatioVo.getCPhaseCount());
                poC.setDeviceMeteredTime(new Date());
                poC.setBaseAmmeteMeteredElectricity(result);
                poC.setBaseAmmeteTime(new Date());
                poC.setCalibrationFactor(cPhaseRatio);
                poC.setCalibrationStartTime(calibrationStartTime);
                poC.setCalibrationEndTime(calibrationEndTime);
                insertList.add(poC);

                mapper.batchUpdateDeviceCalibrationDetail(insertList);

                log.info("计算校准详情表 ---- 校准系数结束！！！！！！！！！！！！！！！！");

            }

        });
    }
}
